package com.efast.dmom.mes.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.dmom.mes.dao.entity.EquipCutterPolishedRecordDao;
import com.efast.dmom.mes.entity.EquipCutterPolishedRecordEntity;
import com.efast.dmom.mes.service.EquipCutterPolishedRecordService;
import com.efast.dmom.mes.util.SpringWebUtils;
import com.efast.dmom.mes.util.TimeUtil;

@Service
public class EquipCutterPolishedRecordServiceImpl implements EquipCutterPolishedRecordService {
	
	@Autowired
	private EquipCutterPolishedRecordDao equipCutterPolishedRecordDao;

	@Override
	public void addCutterGrindRecord(List<EquipCutterPolishedRecordEntity> recordList) {
          if (recordList != null && recordList.size() > 0) {
		      for (EquipCutterPolishedRecordEntity entity:recordList) {
		    	  List<EquipCutterPolishedRecordEntity>  recordEntityList= equipCutterPolishedRecordDao.getRecordEntityListBySpareId(entity.getSpareId());
		    	  if (recordEntityList != null && recordEntityList.size() > 0) {
		    		  entity.setCutterPolishedOrder(recordEntityList.get(recordEntityList.size()-1).getCutterPolishedOrder()+1);
		    		  equipCutterPolishedRecordDao.update(entity);
		    	  } else {
		    		  entity.setCutterPolishedOrder(1); 
		    	  }
		      }
		      equipCutterPolishedRecordDao.saveBatch(recordList); 
	      }
	}

	//刀具打磨后次数增加
	@Override
	public void increasePolishedUsedTimes(int submittedQty, long spareId) {
		EquipCutterPolishedRecordEntity data = equipCutterPolishedRecordDao.getCutterPolishedRecordBySpareId(spareId);
		int qty = data.getCutterPolishedUsedTimes();	
		data.setCutterPolishedUsedTimes(qty+submittedQty);;
		data.setLastModifiedTime(TimeUtil.getLocalDateTime());
		data.setLastModifier(SpringWebUtils.getRequestUser());
		equipCutterPolishedRecordDao.updatePolishedUsedTimes(data);
	}
}	
